package routers

import (
	"bytes"
	"encoding/json"
	"fmt"
	"net/http"
	"strconv"
	"time"

	"shqsoft.cn/pasnj/base"
	"shqsoft.cn/pasnj/entity"
	"shqsoft.cn/pasnj/popedom"

	//log "github.com/sirupsen/logrus"

	jwt "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
)

//对公账户认定
// func AccountCompDiviRouter(c echo.Context) error {
// 	user := c.Get("user").(*jwt.Token)
// 	claims := user.Claims.(*UserLoginClaims)
// 	userid := strconv.Itoa(claims.UserID)
// 	var acids []int
// 	jsonStr := c.FormValue("Acids")
// 	if jsonStr == "" {
// 		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空！")
// 	}
// 	err := json.Unmarshal([]byte(jsonStr), &acids)
// 	if err != nil {
// 		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
// 	}
// 	if l := len(acids); l == 0 {
// 		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空数组！")
// 	}
// 	jsonStr = c.FormValue("DataSource")
// 	if jsonStr == "" {
// 		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
// 	}
// 	var dataSource []entity.Comp_Divi
// 	err = json.Unmarshal([]byte(jsonStr), &dataSource)
// 	if err != nil {
// 		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
// 	}
// 	if l := len(dataSource); l == 0 {
// 		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
// 	}
// 	dsdt := dataSource[0].DSDT
// 	//查询
// 	var updateList []entity.Comp_Divi
// 	tx := base.DB.Begin()
// 	var backdate string
// 	rows, err := tx.Raw("SELECT dbo.GetBackDATEorDSDT(?)", dsdt).Rows()
// 	if err != nil {
// 		tx.Rollback()
// 		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
// 	}
// 	if rows.Next() {
// 		rows.Scan(&backdate)
// 	}
// 	if backdate == "NO" {
// 		tx.Rollback()
// 		return c.JSON(http.StatusOK, "分户日期不能小于最早可追溯日期")
// 	}
// 	err = base.DB.Where("ACID in (?) and ISWO = 0 and (WODT is null or WODT > ?)", acids, dsdt).Find(&updateList).Error
// 	if err != nil {
// 		tx.Rollback()
// 		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
// 	}
// 	//终止现有认定
// 	for _, upateData := range updateList {
// 		upateData.WODT = &dsdt
// 		upateData.WOEM = &userid
// 		if upateData.DSDT.Unix() >= dsdt.Unix() {
// 			upateData.ISWO = true
// 		}
// 		err = base.DB.Model(&(entity.Comp_Divi{})).Save(&upateData).Error
// 		if err != nil {
// 			tx.Rollback()
// 			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
// 		}
// 	}
// 	//新增认定
// 	for _, acid := range acids {
// 		for _, data := range dataSource {
// 			newData := entity.Comp_Divi{
// 				ACID: acid,
// 				EMID: data.EMID,
// 				DSVL: data.DSVL,
// 				DSDT: data.DSDT,
// 				DSTY: 1,
// 				DSEM: userid,
// 				ISWO: false,
// 			}
// 			err = base.DB.Create(&newData).Error
// 			if err != nil {
// 				tx.Rollback()
// 				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
// 			}
// 		}
// 	}
// 	base.DB.Exec("UPDATE T_COMP_ACCO SET ISDIVI=? WHERE ID IN (?)", 1, acids)
// 	tx.Commit()
// 	return c.JSON(http.StatusOK, "OK")
// }

func AccountGetSetIsmrOKDiviRouter(c echo.Context) error {
	acid := c.FormValue("Acid")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_COMP_ACCO SET ISMR=0 WHERE CUID IN(SELECT CUID FROM T_COMP_ACCO WHERE ID=?)", acid)
	tx.Exec("UPDATE T_COMP_ACCO SET ISMR=1 WHERE ID=?", acid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func AccountGetSetIsmrNODiviRouter(c echo.Context) error {
	acid := c.FormValue("Acid")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_COMP_ACCO SET ISMR=0 WHERE CUID IN(SELECT CUID FROM T_COMP_ACCO WHERE ID=?)", acid)
	tx.Exec("UPDATE T_COMP_ACCO SET ISMR=0 WHERE ID=?", acid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func AccountGetSetIndiIsmrOKDiviRouter(c echo.Context) error {
	acid := c.FormValue("Acid")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_INDI_ACCO SET ISMR=0 WHERE CUID IN(SELECT CUID FROM T_INDI_ACCO WHERE ID=?)", acid)
	tx.Exec("UPDATE T_INDI_ACCO SET ISMR=1 WHERE ID=?", acid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func AccountGetSetIndiIsmrNODiviRouter(c echo.Context) error {
	acid := c.FormValue("Acid")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_INDI_ACCO SET ISMR=0 WHERE CUID IN(SELECT CUID FROM T_INDI_ACCO WHERE ID=?)", acid)
	tx.Exec("UPDATE T_INDI_ACCO SET ISMR=0 WHERE ID=?", acid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// AccountCompDiviRouter 对公账户认定
func AccountCompDiviRouter(c echo.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	userid := strconv.Itoa(claims.UserID)
	var acids []int
	jsonStr := c.FormValue("Acid")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空！")
	}
	err := json.Unmarshal([]byte(jsonStr), &acids)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(acids); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空数组！")
	}
	jsonStr = c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.Comp_Divi
	err = json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	dsdt := dataSource[0].DSDT
	//查询
	var updateList []entity.Comp_Divi
	tx := base.DB.Begin()
	var backdate string
	rows, err := tx.Raw("SELECT dbo.GetBackDATEorDSDT(?)", dsdt).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if rows.Next() {
		rows.Scan(&backdate)
	}
	if backdate == "NO" {
		tx.Rollback()
		return c.JSON(http.StatusOK, "分户日期不能小于最早可追溯日期")
	}
	err = base.DB.Where("ACID in (?) and ISWO = 0 and (WODT is null or WODT > ?)", acids, dsdt).Find(&updateList).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	//终止现有认定
	for _, upateData := range updateList {
		upateData.WODT = &dsdt
		upateData.WOEM = &userid
		// if upateData.DSDT.Unix() >= dsdt.Unix() {
		// 	upateData.ISWO = true
		// }
		err = base.DB.Model(&(entity.Comp_Divi{})).Save(&upateData).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	//新增认定
	for _, acid := range acids {
		for _, data := range dataSource {
			newData := entity.Comp_Divi{
				ACID: acid,
				EMID: data.EMID,
				DSVL: data.DSVL,
				DSDT: data.DSDT,
				DSTY: 1,
				DSEM: userid,
				ISWO: false,
			}
			err = base.DB.Create(&newData).Error
			if err != nil {
				tx.Rollback()
				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
			}
		}
	}
	base.DB.Exec("UPDATE T_COMP_ACCO SET ISDIVI=? WHERE ID IN (?)", 1, acids)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// AccountCompkhDiviRouter 对公按客户认定
func AccountCompkhDiviRouter(c echo.Context) error {
	var acids []int
	jsonStr := c.FormValue("Acid")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空！")
	}
	err := json.Unmarshal([]byte(jsonStr), &acids)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(acids); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空数组！")
	}
	jsonStr = c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.Comp_Divi
	err = json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	dsdt := dataSource[0].DSDT
	tx := base.DB.Begin()
	var backdate string
	rows, err := tx.Raw("SELECT dbo.GetBackDATEorDSDT(?)", dsdt).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if rows.Next() {
		rows.Scan(&backdate)
	}
	if backdate == "NO" {
		tx.Rollback()
		return c.JSON(http.StatusOK, "分户日期不能小于最早可追溯日期")
	}
	var buff bytes.Buffer
	for _, data := range dataSource {
		buff.WriteString("" + strconv.Itoa(data.EMID) + "")
		buff.WriteString(":")
		buff.WriteString("" + strconv.FormatFloat(float64(data.DSVL), 'f', 4, 64) + "")
		buff.WriteString(";")
	}
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	gxid := 0
	pTypes, err := popedom.GetUserPermissionTypes(claims.UserID, 2011)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if !base.IsInSlice(pTypes, "4") && base.IsInSlice(pTypes, "O") {
		row := tx.Raw("select PDID FROM DIM_USER WHERE EMID=?", claims.UserID).Row()
		err = row.Scan(&gxid)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	tx.Exec("UP_COMP_DIVI_PL ?,?,?,?,?", dsdt, buff.String(), acids, 1, gxid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 对公其他指标认定
func AccountCompDiviQtzbRouter(c echo.Context) error {
	jlid := c.FormValue("Jlid")
	ywid := c.FormValue("Ywid")
	num := c.FormValue("Num")
	if num == "1" {
		err := base.DB.Exec("DELETE FROM T_COMP_QTZB_DIVI WHERE JLID=? AND YWID=?", jlid, ywid).Error
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "删除认定失败！")
		} else {
			return c.JSON(http.StatusOK, "OK")
		}
	} else {
		jsonStr := c.FormValue("DataSource")
		if jsonStr == "" {
			return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
		}
		var dataSource []entity.COMP_QTZB_DIVI
		err := json.Unmarshal([]byte(jsonStr), &dataSource)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
		if l := len(dataSource); l == 0 {
			return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
		}
		var COMP_QTZB_DIVI []entity.COMP_QTZB_DIVI
		tx := base.DB.Begin()
		id, err := strconv.Atoi(jlid)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
		}
		yid, err := strconv.Atoi(ywid)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
		}
		//删除现有认定
		err = base.DB.Where("JLID = ? AND YWID=?", id, yid).Delete(&COMP_QTZB_DIVI).Error
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "删除现有认定失败！")
		}
		//新增认定
		for _, data := range dataSource {
			newData := entity.COMP_QTZB_DIVI{
				JLID: id,
				EMID: data.EMID,
				DSVL: data.DSVL,
				YWID: data.YWID,
			}
			err = base.DB.Create(&newData).Error
			if err != nil {
				tx.Rollback()
				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
			}
		}
		tx.Commit()
		return c.JSON(http.StatusOK, "OK")
	}

}

// 零售账户认定
func AccountIndiDiviRouter(c echo.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	userid := strconv.Itoa(claims.UserID)

	var acids []int
	jsonStr := c.FormValue("Acid")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空！")
	}
	err := json.Unmarshal([]byte(jsonStr), &acids)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(acids); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空数组！")
	}
	jsonStr = c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.Indi_Divi
	err = json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	dsdt := dataSource[0].DSDT
	//查询
	var updateList []entity.Indi_Divi
	tx := base.DB.Begin()
	var backdate string
	rows, err := tx.Raw("SELECT (?)", dsdt).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if rows.Next() {
		rows.Scan(&backdate)
	}
	if backdate == "NO" {
		tx.Rollback()
		return c.JSON(http.StatusOK, "分户日期不能小于最早可追溯日期")
	}
	err = base.DB.Where("ACID in (?) and ISWO = 0 and (WODT is null or WODT > ?)", acids, dsdt).Find(&updateList).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	//终止现有认定
	for _, upateData := range updateList {
		upateData.WODT = &dsdt
		upateData.WOEM = &userid
		// if upateData.DSDT.Unix() >= dsdt.Unix() {
		// 	upateData.ISWO = true
		// }
		err = base.DB.Model(&(entity.Indi_Divi{})).Save(&upateData).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	//新增认定
	for _, acid := range acids {
		for _, data := range dataSource {
			newData := entity.Indi_Divi{
				ACID: acid,
				EMID: data.EMID,
				DSVL: data.DSVL,
				DSDT: data.DSDT,
				DSTY: 1,
				DSEM: userid,
				ISWO: false,
			}
			err = base.DB.Create(&newData).Error
			if err != nil {
				tx.Rollback()
				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
			}
		}
	}
	base.DB.Exec("UPDATE T_INDI_ACCO SET ISDIVI=? WHERE ID IN (?)", 1, acids)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func AccountIndikhDiviRouter(c echo.Context) error {
	var acids []int
	jsonStr := c.FormValue("Acid")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空！")
	}
	err := json.Unmarshal([]byte(jsonStr), &acids)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(acids); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空数组！")
	}
	jsonStr = c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.Comp_Divi
	err = json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	dsdt := dataSource[0].DSDT
	tx := base.DB.Begin()
	var backdate string
	rows, err := tx.Raw("SELECT dbo.GetBackDATEorDSDT(?)", dsdt).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if rows.Next() {
		rows.Scan(&backdate)
	}
	if backdate == "NO" {
		tx.Rollback()
		return c.JSON(http.StatusOK, "分户日期不能小于最早可追溯日期")
	}
	var buff bytes.Buffer
	for _, data := range dataSource {
		buff.WriteString("" + strconv.Itoa(data.EMID) + "")
		buff.WriteString(":")
		buff.WriteString("" + strconv.FormatFloat(float64(data.DSVL), 'f', 2, 64) + "")
		buff.WriteString(";")
	}
	tx.Exec("UP_INDI_DIVI_PL_CX ?,?,?,?", dsdt, buff.String(), acids, 1)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 零售其他指标认定
func AccountIndiDiviQtzbRouter(c echo.Context) error {
	jlid := c.FormValue("Jlid")
	ywid := c.FormValue("Ywid")
	num := c.FormValue("Num")
	if num == "1" {
		err := base.DB.Exec("DELETE FROM T_INDI_QTZB_DIVI WHERE JLID=? AND YWID=?", jlid, ywid).Error
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "删除认定失败！")
		} else {
			return c.JSON(http.StatusOK, "OK")
		}
	} else {
		jsonStr := c.FormValue("DataSource")
		if jsonStr == "" {
			return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
		}
		var dataSource []entity.COMP_QTZB_DIVI
		err := json.Unmarshal([]byte(jsonStr), &dataSource)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
		if l := len(dataSource); l == 0 {
			return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
		}
		var INDI_QTZB_DIVI []entity.INDI_QTZB_DIVI
		tx := base.DB.Begin()
		id, err := strconv.Atoi(jlid)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
		}
		yid, err := strconv.Atoi(ywid)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
		}
		//删除现有认定
		err = base.DB.Where("JLID = ? AND YWID=?", id, yid).Delete(&INDI_QTZB_DIVI).Error
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "删除现有认定失败！")
		}
		//新增认定
		for _, data := range dataSource {
			newData := entity.INDI_QTZB_DIVI{
				JLID: id,
				EMID: data.EMID,
				DSVL: data.DSVL,
				YWID: data.YWID,
			}
			err = base.DB.Create(&newData).Error
			if err != nil {
				tx.Rollback()
				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
			}
		}
		tx.Commit()
		return c.JSON(http.StatusOK, "OK")
	}

}

// 代发单位认定
func AccountDfdwDiviRouter(c echo.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	userid := strconv.Itoa(claims.UserID)
	var cuids []int
	jsonStr := c.FormValue("Cuids")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "Cuids不能为空！")
	}
	err := json.Unmarshal([]byte(jsonStr), &cuids)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(cuids); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "Cuids不能为空数组！")
	}
	jsonStr = c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.Dfdw_Divi
	err = json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	dsdt := dataSource[0].DSDT
	//查询
	var updateList []entity.Dfdw_Divi
	tx := base.DB.Begin()
	err = base.DB.Where("CUID in (?) and ISWO = 0 and (WODT is null or WODT > ?)", cuids, dsdt).Find(&updateList).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	//终止现有认定
	for _, upateData := range updateList {
		upateData.WODT = &dsdt
		upateData.WOEM = &userid
		if upateData.DSDT.Unix() >= dsdt.Unix() {
			upateData.ISWO = true
		}
		err = base.DB.Model(&(entity.Dfdw_Divi{})).Save(&upateData).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	//新增认定
	for _, cuid := range cuids {
		for _, data := range dataSource {
			newData := entity.Dfdw_Divi{
				CUID: cuid,
				EMID: data.EMID,
				DSVL: data.DSVL,
				DSDT: data.DSDT,
				DSTY: 1,
				DSEM: userid,
				ISWO: false,
			}
			err = base.DB.Create(&newData).Error
			if err != nil {
				tx.Rollback()
				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
			}
		}
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 国际业务认定
func AccountGjywDiviQtzbRouter(c echo.Context) error {
	jlid := c.FormValue("Jlid")
	ywid := c.FormValue("Ywid")
	num := c.FormValue("Num")
	if num == "1" {
		err := base.DB.Exec("DELETE FROM T_CM_INTER_STL_SUM_M_DIVI WHERE JLID=? AND YWID=?", jlid, ywid).Error
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "删除认定失败！")
		} else {
			return c.JSON(http.StatusOK, "OK")
		}
	} else {
		jsonStr := c.FormValue("DataSource")
		if jsonStr == "" {
			return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
		}
		var dataSource []entity.YJRD
		err := json.Unmarshal([]byte(jsonStr), &dataSource)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
		if l := len(dataSource); l == 0 {
			return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
		}
		var YJRD []entity.YJRD
		tx := base.DB.Begin()
		id, err := strconv.Atoi(jlid)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
		}
		yid, err := strconv.Atoi(ywid)
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
		}
		//删除现有认定
		err = base.DB.Where("JLID = ? AND YWID=?", id, yid).Delete(&YJRD).Error
		if err != nil {
			return echo.NewHTTPError(http.StatusBadRequest, "删除现有认定失败！")
		}
		//新增认定
		for _, data := range dataSource {
			newData := entity.YJRD{
				Jlid: id,
				EmID: data.EmID,
				Dsvl: data.Dsvl,
				Ywid: data.Ywid,
			}
			err = base.DB.Create(&newData).Error
			if err != nil {
				tx.Rollback()
				return echo.NewHTTPError(http.StatusBadRequest, err.Error())
			}
		}
		tx.Commit()
		return c.JSON(http.StatusOK, "OK")
	}

}

// AccountCompClieQylxRouter 对公客户企业类型维护
func AccountCompClieQylxRouter(c echo.Context) error {
	jlid := c.FormValue("Jlid")
	jsonStr := c.FormValue("DataSource")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空！")
	}
	var dataSource []entity.CompClieQylxWh
	err := json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "DataSource不能为空数组！")
	}
	var CompClieQylxWh []entity.CompClieQylxWh
	tx := base.DB.Begin()
	id, err := strconv.Atoi(jlid)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	//删除现有企业类型
	err = base.DB.Where("JLID = ? ", id).Delete(&CompClieQylxWh).Error
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "删除现有企业类型失败！")
	}
	//新增企业类型
	for _, data := range dataSource {
		newData := entity.CompClieQylxWh{
			JLID: id,
			QYLX: data.QYLX,
			GJLX: data.GJLX,
		}
		err = base.DB.Create(&newData).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// UpdateCompAccoInrtRouter 对公利率维护
func UpdateCompAccoInrtRouter(c echo.Context) error {
	acid, err := strconv.Atoi(c.FormValue("acid"))
	ll := c.FormValue("ll")
	datestr := c.FormValue("date")
	date, err := time.Parse("2006-1-2", datestr)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx := base.DB.Begin()
	err = tx.Exec("UPDATE T_COMP_ACCO SET INRT=? WHERE ID=?", ll, acid).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	err = tx.Exec("UPDATE T_V_COMP_ACCO SET 利率=? WHERE DTID>=? AND ID=?", ll, date, acid).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	prams := make([]interface{}, 3)
	prams[0] = acid
	prams[1] = date.AddDate(-1, 0, 0)
	prams[2] = ll
	err = tx.Exec("EXEC UP_COMP_ACCO_LLWH ?,?,?", prams[0], prams[1], prams[2]).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// SetCompAccoIsysRouter 对公存款原生设置
func SetCompAccoIsysRouter(c echo.Context) error {
	var dateString string
	acids := c.FormValue("acids")
	isysStr := c.FormValue("isys")
	date := c.FormValue("date")
	err := json.Unmarshal([]byte(date), &dateString)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	isys := "0"
	if isysStr == "true" {
		isys = "1"
	}
	tx := base.DB.Begin()
	prams := make([]interface{}, 3)
	prams[0] = acids
	prams[1] = isys
	prams[2] = dateString
	tx.Exec("EXEC UP_COMP_YSPS_SET ?,?,?", prams[0], prams[1], prams[2])
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// IndiAccoDiviSqRouter 个人存款认定申请
func IndiAccoDiviSqRouter(c echo.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	userid := strconv.Itoa(claims.UserID)
	uid, e := strconv.Atoi(userid)
	if e != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "类型转换失败")
	}
	jsonStr := c.FormValue("dataSource")

	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "dataSource不能为空！")
	}
	var dataSource []entity.IndiAccoDiviSq
	err := json.Unmarshal([]byte(jsonStr), &dataSource)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(dataSource); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "dataSource不能为空数组！")
	}

	jsonStr2 := c.FormValue("mainData")
	if jsonStr2 == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "mainData不能为空！")
	}
	var mainData []entity.IndiAccoDivi
	err = json.Unmarshal([]byte(jsonStr2), &mainData)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(mainData); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "mainData不能为空数组！")
	}
	cuid := mainData[0].ID
	tx := base.DB.Begin()
	for _, data := range dataSource {
		newData := entity.IndiAccoDiviSq{
			ACID:  cuid,
			EMID:  data.EMID,
			DSVL:  data.DSVL,
			DSDT:  data.DSDT,
			SQRID: uid,
			KHWD:  mainData[0].Khwd,
			GXBM:  mainData[0].Gxbm,
			KHR:   mainData[0].Khr,
			// ZH:    mainData[0].Zhanghao,
			// KH:    mainData[0].Khao,
			KHMC: mainData[0].Khmc,
			ZJHM: mainData[0].Sfzh,
			TJRQ: time.Now(),
			ISTY: "0",
			SPSJ: time.Date(1900, 1, 1, 0, 0, 0, 0, time.Local),
		}
		err = tx.Create(&newData).Error

		tx.Exec("UPDATE T_INDI_ACCO_SPB SET ParentOrganID=B.ID FROM T_INDI_ACCO_SPB A INNER JOIN T_Organization B ON A.GXBM=B.OrganName WHERE ACID=?", cuid)
		tx.Exec("UPDATE T_INDI_ACCO_SPB SET OrganID=B.ID FROM T_INDI_ACCO_SPB A INNER JOIN T_Organization B ON A.KHWD=B.OrganName WHERE ACID=?", cuid)

		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}

	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// IndiAccoDiviSqBTYRouter 个人存款认定申请审批不同意
func IndiAccoDiviSqBTYRouter(c echo.Context) error {
	var acids []int
	jsonStr := c.FormValue("acids")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空！")
	}
	err := json.Unmarshal([]byte(jsonStr), &acids)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(acids); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "Acids不能为空数组！")
	}
	yy := c.FormValue("yy")
	tx := base.DB.Begin()
	for _, acid := range acids {
		err = tx.Exec("UPDATE T_INDI_ACCO_SPB SET ISTY='2',SPSJ=?,YY=? WHERE ID IN(?) ", time.Now(), yy, acid).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// IndiAccoDiviSqTYRouter 个人存款认定申请审批同意
func IndiAccoDiviSqTYRouter(c echo.Context) error {
	var acids []int
	jsonStr := c.FormValue("keys")
	if jsonStr == "" {
		return echo.NewHTTPError(http.StatusBadRequest, "keys不能为空！")
	}
	err := json.Unmarshal([]byte(jsonStr), &acids)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if l := len(acids); l == 0 {
		return echo.NewHTTPError(http.StatusBadRequest, "keys不能为空数组！")
	}

	tx := base.DB.Begin()
	//查询ID的数据信息
	var List []entity.IndiAccoDiviSq
	err = base.DB.Where("ID in (?)", acids).Find(&List).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	//更新审批表状态
	for _, acid := range acids {
		err = tx.Exec("UPDATE T_INDI_ACCO_SPB SET ISTY='1',SPSJ=?,YY='同意' WHERE ID IN(?) ", time.Now(), acid).Error
		if err != nil {
			tx.Rollback()
			return echo.NewHTTPError(http.StatusBadRequest, err.Error())
		}
	}
	//插入分户表
	for _, data := range List {
		tx.Exec("INSERT INTO T_INDI_ACCO_DIVI(ACID,DSTY,EMID,DSVL,DSDT,DSEM,ISWO)"+
			"SELECT ID,1,?,?,?,'SP',0"+
			"FROM T_INDI_ACCO WHERE CUID=?", data.EMID, data.DSVL/100, data.DSDT, data.ACID)
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// ScdgzhsysjRouter 生成对公综合收益目标数据
func ScdgzhsysjRouter(c echo.Context) error {
	var dateString string
	jsonStr := c.FormValue("Date")
	err := json.Unmarshal([]byte(jsonStr), &dateString)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx := base.DB.Begin()
	if dateString != "" {
		err = tx.Exec("EXEC UP_COMP_ZHSY_WCQK ?", dateString).Error
	} else {
		err = tx.Exec("EXEC UP_COMP_ZHSY_WCQK").Error
	}
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// SczxzhsysjRouter 生成中小综合收益目标数据
func SczxzhsysjRouter(c echo.Context) error {
	var dateString string
	jsonStr := c.FormValue("Date")
	err := json.Unmarshal([]byte(jsonStr), &dateString)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx := base.DB.Begin()
	err = tx.Exec("EXEC UP_COMP_CLIE_ZXQY_YJGLTJ_DEL").Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// AddUserEmailRouter 对公客户综合收益邮件发送人
func AddUserEmailRouter(c echo.Context) error {
	emid := c.FormValue("emid")
	tx := base.DB.Begin()
	var (
		SysUserID   string
		SysUserName string
		OrganName   string
		EMail       string
	)
	rows, err := tx.Raw("SELECT A.SysUserID,A.SysUserName,B.OrganName,A.EMail FROM T_SysUser A INNER JOIN T_Organization B ON A.OrganID=B.ID WHERE A.ID=?", emid).Rows()
	if rows.Next() {
		rows.Scan(&SysUserID, &SysUserName, &OrganName, &EMail)
	}
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Exec("INSERT INTO T_COMP_ZHSY_USER_MAIL(ID,EMNO,EMNM,UTNM,EMAIL) VALUES(?,?,?,?,?)", emid, SysUserID, SysUserName, OrganName, EMail)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// DelUserEmailRouter 对公客户综合删除收益邮件发送人
func DelUserEmailRouter(c echo.Context) error {
	emid := c.FormValue("emid")
	tx := base.DB.Begin()
	tx.Exec("DELETE FROM T_COMP_ZHSY_USER_MAIL WHERE ID=?", emid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// sendMailRouter 对公客户综合删除收益邮件发送人
func sendMailRouter(c echo.Context) error {
	tx := base.DB.Begin()
	var EMail string
	rows, err := tx.Raw("SELECT EMail FROM T_COMP_ZHSY_USER_MAIL").Rows()
	for rows.Next() {
		rows.Scan(&EMail)
	}
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Exec("exec UP_COMP_ZHSY_EMAIL")
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// AddZxqyUserEmailRouter 中小客户综合收益邮件发送人
func AddZxqyUserEmailRouter(c echo.Context) error {
	emid := c.FormValue("emid")
	tx := base.DB.Begin()
	var (
		SysUserID   string
		SysUserName string
		OrganName   string
		EMail       string
	)
	rows, err := tx.Raw("SELECT A.SysUserID,A.SysUserName,B.OrganName,A.EMail FROM T_SysUser A INNER JOIN T_Organization B ON A.OrganID=B.ID WHERE A.ID=?", emid).Rows()
	if rows.Next() {
		rows.Scan(&SysUserID, &SysUserName, &OrganName, &EMail)
	}
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Exec("INSERT INTO T_COMP_ZXQY_ZHSY_USER_MAIL(ID,EMNO,EMNM,UTNM,EMAIL) VALUES(?,?,?,?,?)", emid, SysUserID, SysUserName, OrganName, EMail)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// DelUserEmailRouter 中小客户综合删除收益邮件发送人
func DelZxqyUserEmailRouter(c echo.Context) error {
	emid := c.FormValue("emid")
	tx := base.DB.Begin()
	tx.Exec("DELETE FROM T_COMP_ZXQY_ZHSY_USER_MAIL WHERE ID=?", emid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// sendMailRouter 中小客户综合删除收益邮件发送人
func sendZxqyMailRouter(c echo.Context) error {
	tx := base.DB.Begin()
	var EMail string
	rows, err := tx.Raw("SELECT EMail FROM T_COMP_ZXQY_ZHSY_USER_MAIL").Rows()
	for rows.Next() {
		rows.Scan(&EMail)
	}
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Exec("exec UP_COMP_ZXQY_ZHSY_EMAIL")
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// defaultDateRouter 获取追溯时间
func defaultDateRouter(c echo.Context) error {
	tx := base.DB.Begin()
	var date string
	rows, _ := tx.Raw("select CAST(CONVERT(date,dbo.GetBackDATE(),112) AS NVARCHAR(10))").Rows()
	for rows.Next() {
		rows.Scan(&date)
	}
	tx.Commit()
	return c.JSON(http.StatusOK, date)
}

func InsertListRouter(c echo.Context) error {
	tx := base.DB.Begin()
	tx.Exec("UP_V_COMP_ACCO")
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func InsertListDateRouter(c echo.Context) error {
	var dateString string
	jsonStr := c.FormValue("date")
	err := json.Unmarshal([]byte(jsonStr), &dateString)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx := base.DB.Begin()
	tx.Exec("UP_V_COMP_ACCO ?", dateString)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func GetSysUserNameRouter(c echo.Context) error {
	var name string
	jsonStr := c.FormValue("name")
	err := json.Unmarshal([]byte(jsonStr), &name)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx := base.DB.Begin()
	var emno string
	rows, err := tx.Raw("SELECT SysUserID NAME FROM T_SysUser WHERE SysUserName= ?", name).Rows()
	for rows.Next() {
		rows.Scan(&emno)
	}
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	flag := "0"
	if emno != "" {
		flag = "1"
	}
	tx.Commit()
	return c.JSON(http.StatusOK, flag)
}

// 删除对公存款认定关系
func DelCompDiviRouter(c echo.Context) error {
	id := c.FormValue("id")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_COMP_ACCO_DIVI SET WODT=dbo.GetCurSysDate(),ISWO=1 WHERE ID=?", id)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 删除对公贷款认定关系
func DelCompDiviDkRouter(c echo.Context) error {
	id := c.FormValue("id")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_COMP_ACCO_DIVI SET WODT=dbo.GetCurSysDate(),ISWO=1 WHERE ID=?", id)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 删除零售认定关系
func DelIndiDiviRouter(c echo.Context) error {
	id := c.FormValue("id")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_INDI_ACCO_DIVI SET WODT=dbo.GetCurSysDate(),ISWO=1 WHERE ID=?", id)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 删除个人贷款认定关系
func DeleteINDILOANRouter(c echo.Context) error {
	id := c.FormValue("id")
	tx := base.DB.Begin()
	tx.Exec("UPDATE T_INDI_LOAN_DIVI SET WODT=dbo.GetCurSysDate(),ISWO=1 WHERE ID=?", id)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 删除对公认定关系
func GetFTPRouter(c echo.Context) error {
	key := c.FormValue("key")
	var (
		ftp  string
		days float32
	)
	row := base.DB.Raw("SELECT dbo.GetFTPVal(?)", key).Row()
	row.Scan(&ftp)
	row = base.DB.Raw("SELECT ts from t_conf_para where pano=?", key).Row()
	row.Scan(&days)
	// if days < 30 {
	// 	days = 365
	// }
	retVal := fmt.Sprintf(`{"ftp":%s,"days":%d}`, ftp, int(days))
	return c.String(http.StatusOK, retVal)
}
func GetMnlrRouter(c echo.Context) error {
	ywpz := c.FormValue("ywpz")
	ftpcs := c.FormValue("ftpcs")
	ksrq := c.FormValue("ksrq")
	jsrq := c.FormValue("jsrq")
	je := c.FormValue("je")
	ll := c.FormValue("ll")
	ftp := c.FormValue("ftp")
	tx := base.DB.Begin()
	tx.Exec("EXEC UP_BASE_MNLR ?,?,?,?,?,?,?", ywpz, ftpcs, ksrq, jsrq, je, ll, ftp)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func GetCPQZRouter(c echo.Context) error {
	key := c.FormValue("cpqz")
	tx := base.DB.Begin()
	var (
		cpqz string
	)
	rows, err := tx.Raw("SELECT dbo.GetCPQZ(?)", key).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&cpqz)
	}
	tx.Commit()
	return c.String(http.StatusOK, cpqz)
}

func GetKHPZRouter(c echo.Context) error {
	key := c.FormValue("khpz")
	tx := base.DB.Begin()
	var (
		khpz string
	)
	rows, err := tx.Raw("SELECT dbo.GetKHPJ(?)", key).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&khpz)
	}
	tx.Commit()
	return c.String(http.StatusOK, khpz)
}

func GetZYDBRouter(c echo.Context) error {
	key := c.FormValue("zydb")
	tx := base.DB.Begin()
	var (
		zydb string
	)
	rows, err := tx.Raw("SELECT dbo.GetZYDB(?)", key).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&zydb)
	}
	tx.Commit()
	return c.String(http.StatusOK, zydb)
}

func GetBZDBRouter(c echo.Context) error {
	key1 := c.FormValue("bzlx")
	key2 := c.FormValue("bzrpj")
	tx := base.DB.Begin()
	var (
		bzdbxs string
	)
	rows, err := tx.Raw("SELECT dbo.GetBZDB(?,?)", key1, key2).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&bzdbxs)
	}
	tx.Commit()
	return c.String(http.StatusOK, bzdbxs)
}

func GetQXRouter(c echo.Context) error {
	key := c.FormValue("qx")
	tx := base.DB.Begin()
	var (
		qx string
	)
	rows, err := tx.Raw("SELECT dbo.GetQX(?)", key).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&qx)
	}
	tx.Commit()
	return c.String(http.StatusOK, qx)
}

func GetGMRouter(c echo.Context) error {
	key := c.FormValue("gm")
	tx := base.DB.Begin()
	var (
		gm string
	)
	rows, err := tx.Raw("SELECT dbo.GetGM(?)", key).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&gm)
	}
	tx.Commit()
	return c.String(http.StatusOK, gm)
}

func GetHYRouter(c echo.Context) error {
	key := c.FormValue("hy")
	tx := base.DB.Begin()
	var (
		hy string
	)
	rows, err := tx.Raw("SELECT dbo.GetHY(?)", key).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&hy)
	}
	tx.Commit()
	return c.String(http.StatusOK, hy)
}
func GetEVARouter(c echo.Context) error {
	cpqzxs := c.FormValue("cpqzxs")
	khpzxs := c.FormValue("khpzxs")
	zydbxs := c.FormValue("zydbxs")
	bzdbxs := c.FormValue("bzdbxs")
	qxxs := c.FormValue("qxxs")
	gmxs := c.FormValue("gmxs")
	hyxs := c.FormValue("hyxs")
	je := c.FormValue("je")
	khlr := c.FormValue("khlr")
	bzjbl := c.FormValue("bzjbl")
	tx := base.DB.Begin()
	tx.Exec("EXEC UP_BASE_EVA ?,?,?,?,?,?,?,?,?,?", cpqzxs, khpzxs, zydbxs, bzdbxs, qxxs, gmxs, hyxs, je, khlr, bzjbl)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

func SetUseLogRouter(c echo.Context) error {
	user := c.Get("user").(*jwt.Token)
	claims := user.Claims.(*UserLoginClaims)
	userid := strconv.Itoa(claims.UserID)

	keyStr := c.FormValue("key")
	key, err := strconv.Atoi(keyStr)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, "参数Key不能为空")
	}
	tx := base.DB.Begin()
	tx.Exec("EXEC UP_USER_LOG ?,?", key, userid)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 对公汇总流程
func ExecprocRouter(c echo.Context) error {
	tx := base.DB.Begin()
	err := tx.Exec("EXEC UP_COMP_FLOW_RUN_LOG ").Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 零售汇总流程
func ExecindiRouter(c echo.Context) error {
	tx := base.DB.Begin()
	err := tx.Exec("EXEC UP_INDI_FLOW_RUN_LOG ").Error
	if err != nil {
		tx.Rollback()
		return err
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 路由：非保本理财工号维护
func SaveGhRouter(c echo.Context) error {
	acidStr := c.FormValue("acid")
	acid, err := strconv.Atoi(acidStr)
	uidStr := c.FormValue("uid")
	uid, err := strconv.Atoi(uidStr)
	tx := base.DB.Begin()
	err = tx.Exec("update T_INDI_FANA set EMID=? where FAID=? ", uid, acid).Error
	if err != nil {
		return err
	}
	err = tx.Exec("DELETE FROM T_FbbghWH where FAID=? ", acid).Error
	if err != nil {
		return err
	}
	newData := entity.FbbghWH{
		FAID: acid,
		EMID: uid,
	}
	err = tx.Save(&newData).Error
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	tx.Commit()
	return c.JSON(http.StatusOK, "ok")
}

// 路由：零售新客户申请 相关客户经理审批
func IndiClieDiviFlowNewAppRouter(c echo.Context) error {
	emid1 := c.FormValue("emid1")
	emid2 := c.FormValue("emid2")
	emid3 := c.FormValue("emid3")
	frid := c.FormValue("frid")
	tx := base.DB.Begin()
	tx.Exec("insert into T_INDI_XKH_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid1, frid)
	tx.Exec("insert into T_INDI_XKH_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid2, frid)
	tx.Exec("insert into T_INDI_XKH_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid3, frid)

	tx.Exec("delete from T_INDI_XKH_DIVI_XGKHJL where emid=0")
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 路由：零售客户变更 相关客户经理审批
func IndiClieDiviFlowUpdateAppRouter(c echo.Context) error {
	emid1 := c.FormValue("emid1")
	emid2 := c.FormValue("emid2")
	emid3 := c.FormValue("emid3")
	frid := c.FormValue("frid")
	tx := base.DB.Begin()
	tx.Exec("insert into T_INDI_BG_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid1, frid)
	tx.Exec("insert into T_INDI_BG_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid2, frid)
	tx.Exec("insert into T_INDI_BG_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid3, frid)

	tx.Exec("delete from T_INDI_BG_DIVI_XGKHJL where emid=0")
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 路由：公司客户变更 相关客户经理审批
func CompClieDiviFlowUpdateRouter(c echo.Context) error {
	emid1 := c.FormValue("emid1")
	emid2 := c.FormValue("emid2")
	emid3 := c.FormValue("emid3")
	frid := c.FormValue("frid")
	tx := base.DB.Begin()
	tx.Exec("insert into T_COMP_BG_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid1, frid)
	tx.Exec("insert into T_COMP_BG_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid2, frid)
	tx.Exec("insert into T_COMP_BG_DIVI_XGKHJL(EMID,FRID)values(?,?)", emid3, frid)

	tx.Exec("delete from T_COMP_BG_DIVI_XGKHJL where emid=0")
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// 路由：公司中收修改客户名称
func XghmRouter(c echo.Context) error {
	jlid := c.FormValue("jlid")
	cunm := c.FormValue("cunm")
	// xgfs := c.FormValue("xgfs")
	var count int
	flag := "OK"
	tx := base.DB.Begin()
	row := base.DB.Raw("SELECT COUNT(*) FROM T_CM_CORP_CUST_DIM WHERE CUST_NAME=?", cunm).Row()
	err := row.Scan(&count)
	if err != nil {
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	if count <= 0 {
		flag = "NO"
	} else {
		tx.Exec("EXEC UP_COMP_BIZ_FEE_XGHM ?,?", jlid, cunm)
	}
	tx.Commit()
	return c.JSON(http.StatusOK, flag)
}

func XgygRouter(c echo.Context) error {
	jlid := c.FormValue("jlid")
	yg1 := c.FormValue("yg1")
	yg2 := c.FormValue("yg2")
	yg3 := c.FormValue("yg3")
	yg4 := c.FormValue("yg4")
	yg5 := c.FormValue("yg5")
	zb1 := c.FormValue("zb1")
	zb2 := c.FormValue("zb2")
	zb3 := c.FormValue("zb3")
	zb4 := c.FormValue("zb4")
	zb5 := c.FormValue("zb5")
	tx := base.DB.Begin()
	tx.Exec("EXEC UP_COMP_BIZ_FEE_XGYG ?,?,?,?,?,?,?,?,?,?,?", jlid, yg1, yg2, yg3, yg4, yg5, zb1, zb2, zb3, zb4, zb5)
	tx.Commit()
	return c.JSON(http.StatusOK, "OK")
}

// StopUsing
func StopUsings(userID int, status string) error {
	user := entity.User{}
	err := base.DB.Where("ID=?", userID).First(&user).Error
	if err != nil {
		return err
	}
	if status == "1" {
		base.DB.Exec("UPDATE T_SysUser 	SET STATUS=? WHERE ID=?", status, userID)
		base.DB.Exec("UPDATE T_SysUser 	SET SysUserName=CASE WHEN CHARINDEX('（已',SysUserName) <>0 THEN SUBSTRING(SysUserName,0,CHARINDEX('（已',SysUserName)) ELSE SysUserName END WHERE ID=?", userID)
	} else if status == "2" {
		base.DB.Exec("UPDATE T_SysUser 	SET STATUS=? WHERE ID=?", status, userID)
		base.DB.Exec("UPDATE T_SysUser 	SET SysUserName=SysUserName+'（已离职）' WHERE ID=?", userID)
	} else if status == "3" {
		base.DB.Exec("UPDATE T_SysUser 	SET STATUS=? WHERE ID=?", status, userID)
		base.DB.Exec("UPDATE T_SysUser 	SET SysUserName=SysUserName+'（已停用）' WHERE ID=?", userID)
	}

	return nil
}

func StopUsingsGhr(userID int, status string) error {
	user := entity.User{}
	err := base.DB.Where("ID=?", userID).First(&user).Error
	if err != nil {
		return err
	}
	base.DB.Exec("UPDATE T_SysUser 	SET ISLSGHR=? WHERE ID=?", status, userID)

	return nil
}

func GetCompJcRouter(c echo.Context) error {
	rwIDs := c.FormValue("rwIDs")
	tx := base.DB.Begin()
	var (
		hy string
	)
	rows, err := tx.Raw("SELECT dbo.GetHY(?)", rwIDs).Rows()
	if err != nil {
		tx.Rollback()
		return echo.NewHTTPError(http.StatusBadRequest, err.Error())
	}
	defer rows.Close()
	if rows.Next() {
		rows.Scan(&hy)
	}
	tx.Commit()
	return c.String(http.StatusOK, hy)
}
